using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace MobileTDL
{
	public class SettingsBase
	{
		public static T Load<T>(string filename)
			where T : SettingsBase, new()
		{
			T settings;
			if (File.Exists(filename))
			{
				XmlSerializer ser = new XmlSerializer(typeof(T));
				using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
				{
					settings = (T)ser.Deserialize(fs);
				}
			}
			else settings = new T();
			return settings;
		}

		public void Save(string filename)
		{
			string dir = Path.GetDirectoryName(filename);
			if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

			XmlSerializer ser = new XmlSerializer(GetType());
			using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
			{
				ser.Serialize(fs, this);
			}
		}
	}
}
